﻿using System;
using System.Xml;
using System.Xml.XPath;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using TheOutfield.UmbExt.UniversalMediaPicker.Xslt;
using umbraco;

namespace Skybrud.UmbExt.UniversalMediaPicker.Providers.Issuu
{
    public class IssuuXsltExtensions : AbstractXsltExtension 
    {
        /// <summary>
        /// Returns xml for a given Issuu document
        /// </summary>
        /// <param name="docTypeDefinitionId"></param>
        /// <param name="issuuDocId"></param>
        /// <returns></returns>
        public static XPathNodeIterator GetDocument(int docTypeDefinitionId, string issuuDocId)
        {
            var provider = GetProvider<IssuuProvider>(docTypeDefinitionId);
            var service = new IssuuProvider(provider.Config);

            try
            {
                var doc = service.GetIssuuDocument(issuuDocId);
                
                var xd = new XmlDocument();
                var docNode = xmlHelper.addTextNode(xd, "Document", "");

                if (doc != null)
                {
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Id", doc.DocumentId));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Access", doc.Access));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Description", doc.Description));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Folders", doc.Folders.ListToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Name", doc.Name));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "OrgDocName", doc.OrgDocName));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "OrgDocType", doc.OrgDocType));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Origin", doc.Origin));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "PageCount", doc.PageCount));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Publishdate", doc.PublishDate));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "State", doc.State));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Tags", doc.Tags.ListToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Title", doc.Title));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Type", doc.Type));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Username", doc.Username));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "ViewCount", doc.ViewCount));
                }

                xd.AppendChild(docNode);

                // ReSharper disable PossibleNullReferenceException
                return xd.CreateNavigator().Select("/Document");
                // ReSharper restore PossibleNullReferenceException
            }
            catch (Exception e)
            {
                return e.ToXPathNodeIterator();

            }
            
        }

        public static XPathNodeIterator GetPublicDocuments(int docTypeDefinitionId)
        {
            var provider = GetProvider<IssuuProvider>(docTypeDefinitionId);
            var service = new IssuuProvider(provider.Config);

            try
            {
                var docs = service.GetIssuuDocuments();

                var xd = new XmlDocument();
                var docRoot = xmlHelper.addTextNode(xd, "Documents", "");
                
                if (docs != null)
                {
                    foreach (var doc in docs)
                    {
                        var docNode = xmlHelper.addTextNode(xd, "Document", "");
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "Id", doc.DocumentId));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "Access", doc.Access));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "Description", doc.Description));
                        //docNode.AppendChild(xmlHelper.addTextNode(xd, "Folders", doc.Folders.ListToString())); //creates error: Length cannot be less than zero. Parameter name: length 
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "Name", doc.Name));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "OrgDocName", doc.OrgDocName));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "OrgDocType", doc.OrgDocType));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "Origin", doc.Origin));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "PageCount", doc.PageCount));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "Publishdate", doc.PublishDate));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "State", doc.State));
                        //docNode.AppendChild(xmlHelper.addTextNode(xd, "Tags", doc.Tags.ListToString()));  //creates error: Length cannot be less than zero. Parameter name: length 
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "Title", doc.Title));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "Type", doc.Type));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "Username", doc.Username));
                        docNode.AppendChild(xmlHelper.addTextNode(xd, "ViewCount", doc.ViewCount));
                        
                        docRoot.AppendChild(docNode);
                    }

                }

                xd.AppendChild(docRoot);

                return xd.CreateNavigator().Select("/Documents");
            }
            catch (Exception e)
            {
                return e.ToXPathNodeIterator();
            }
        }

        /// <summary>
        /// Returns embedcode for Issuu
        /// </summary>
        /// <param name="docTypeDefinitionId"></param>
        /// <param name="issuuDocId"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static string GetEmbedCode(int docTypeDefinitionId, string issuuDocId, string width, string height)
        {
            var provider = GetProvider<IssuuProvider>(docTypeDefinitionId);
            var returnHtml = new System.Text.StringBuilder();
            var issuuDoc = provider.GetMediaItemById(issuuDocId);

            returnHtml.AppendLine("<object style=\"width:" + width + ";height:" + height + "\">");
            returnHtml.AppendLine(" <param name=\"movie\" value=\"http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf?mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Fcolor%2Flayout.xml&amp;backgroundColor=FFFFFF&amp;showFlipBtn=true&amp;documentId=" + issuuDoc.Id + "&amp;docName=&amp;username=&amp;loadingInfoText=" + issuuDoc.Title + "&amp;et=1285139195165&amp;er=55\" />");
            returnHtml.AppendLine(" <param name=\"allowfullscreen\" value=\"true\"/>");
            returnHtml.AppendLine(" <param name=\"menu\" value=\"false\"/>");
            returnHtml.AppendLine(" <embed src=\"http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" menu=\"false\" style=\"width:" + width + ";height:" + height + "\" flashvars=\"mode=embed&amp;layout=http%3A%2F%2Fskin.issuu.com%2Fv%2Fcolor%2Flayout.xml&amp;backgroundColor=FFFFFF&amp;showFlipBtn=true&amp;documentId=" + issuuDoc.Id + "&amp;docName=&amp;username=&amp;loadingInfoText=" + issuuDoc.Title + "&amp;et=1285139195165&amp;er=55\" />");
            returnHtml.AppendLine("</object>");

            return returnHtml.ToString();
        }
    }
}